package com.title4;

import javax.rmi.CORBA.Stub;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author milian
 * @Date 2021/8/10 0010 20:29
 */
public class title4 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Class clazz = Class.forName("com.title4.Person");
//        Person person = (Person) clazz.newInstance();
        Constructor declaredConstructor = clazz.getDeclaredConstructor(int.class);
        declaredConstructor.setAccessible(true);
        Person person = (Person) declaredConstructor.newInstance(22);
        Field name = clazz.getDeclaredField("name");
        //        设置临时权限
        name.setAccessible(true);
        name.set(person,"zhangsan");
        Method eat = clazz.getMethod("eat", String.class);
        Method getName = clazz.getDeclaredMethod("getName");
        eat.invoke(person,"米饭");
//        设置临时权限
        getName.setAccessible(true);
        Object invoke = getName.invoke(person);
        System.out.println(invoke);


    }
}
